package com.tanhua.server.controller;

import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.PageResultVo;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolderThreadLocal;
import com.tanhua.server.service.SettingsService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PathParam;
import java.util.Map;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/30 0:00
 */

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    /**
     * 查询用户通过设置
     * @return
     */
    @GetMapping("/settings")
    // 拦截器已经将token验证，不需要接受
    public ResponseEntity setting(){

        SettingsVo vo =  settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     * @param map
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){

        Long userId = UserHolderThreadLocal.getId();
        // 将接收到的json数据保存在集合中。
        String content = (String) map.get("content");
        // 保存问题
        settingsService.saveQuestion(userId, content);
        return ResponseEntity.ok(null);
    }


    /**
     * 通用设置推送开关
     * @param settings
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Settings settings){

        // 获取用户id，封装到settings对象
        Long userId = UserHolderThreadLocal.getId();
        settings.setUserId(userId);
        // 调用service设置推送开关状态
        settingsService.updateByUserId(settings);
        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize){

        Long userId = UserHolderThreadLocal.getId();
        PageResultVo vo = settingsService.selectByPage(page, pageSize, userId);
        return ResponseEntity.ok(vo);
    }

    // 取消黑名单
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity blacklistDelete(@PathVariable Long uid){

        settingsService.deleteById(uid);
        return ResponseEntity.ok(null);
    }



}

